/*
 * Copyright (C) 2015 UFreedom
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ufreedom.demo.transition;

import com.ufreedom.demo.UiUtils;
import com.ufreedom.demo.util.AnimatorStateChangedListener;
import com.ufreedom.floatingview.spring.SimpleReboundListener;
import com.ufreedom.floatingview.spring.SpringHelper;
import com.ufreedom.floatingview.transition.BaseFloatingPathTransition;
import com.ufreedom.floatingview.transition.FloatingPath;
import com.ufreedom.floatingview.transition.PathPosition;
import com.ufreedom.floatingview.transition.YumFloating;
import com.ufreedom.floatingview.util.LogUtil;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.render.Path;
import ohos.app.Context;

/**
 * PlaneFloating
 *
 * @author UFreedom
 * @since 2021-03-31
 */
public class PlaneFloating extends BaseFloatingPathTransition {
    private static final float MXCOOR0 = 100;
    private static final float MYCOOR0 = -300;
    private static final float MYCOOR1 = -600;
    private static final float MLINETOYCOOR1 = -100;
    private static final float MPROGRESS = 2110;
    private static final long MDURATION = 3000L;
    private static final double MBOUNCINESS = 14;
    private static final double MSPEED = 15;
    private int mScreenHeight;

    /**
     * PlaneFloating
     *
     * @param context
     */
    public PlaneFloating(Context context) {
        mScreenHeight = UiUtils.getScreenWidth(context);
    }

    /**
     * getFloatingPath
     *
     * @return FloatingPath
     */
    @Override
    public FloatingPath getFloatingPath() {
        Path path = new Path();
        path.moveTo(0, 0);
        path.quadTo(MXCOOR0, MYCOOR0, 0, MYCOOR1);
        path.rLineTo(0, -mScreenHeight + MLINETOYCOOR1);
        return FloatingPath.create(path, false);
    }

    /**
     * applyFloating
     *
     * @param yumFloating
     */
    @Override
    public void applyFloating(final YumFloating yumFloating) {
        AnimatorValue translateAnimator = new AnimatorValue();
        translateAnimator.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float v) {
                PathPosition floatingPosition = getFloatingPosition(v * MPROGRESS);
                yumFloating.setTranslationX(floatingPosition.x);
                yumFloating.setTranslationY(floatingPosition.y);
                LogUtil.error("animatorValue", "x" + floatingPosition.x + " y" + floatingPosition.y);
            }
        });

        translateAnimator.setStateChangedListener(new AnimatorStateChangedListener() {
            @Override
            public void onEnd(Animator animator) {
                yumFloating.setTranslationX(0);
                yumFloating.setTranslationY(0);
                yumFloating.setAlpha(0f);
                yumFloating.clear();
            }
        });
        SpringHelper.createWithBouncinessAndSpeed(0.0f, 1.0f, MBOUNCINESS, MSPEED)
                .reboundListener(new SimpleReboundListener() {
                    @Override
                    public void onReboundUpdate(double currentValue) {
                        yumFloating.setScaleX((float) currentValue);
                        yumFloating.setScaleY((float) currentValue);
                    }
                }).start(yumFloating);

        translateAnimator.setDuration(MDURATION);
        translateAnimator.start();
    }
}